﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using Commonlib;
using Albon.Model;

namespace AlbonWeb.Manager.ClientManager
{
    public partial class ClientPosture : System.Web.UI.Page
    {
        private readonly string username = HttpContext.Current.User.Identity.Name;
        private readonly BLL.ClientBLL clientbll = new BLL.ClientBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            //绑定身形图片数据
            bindRep(repGroup, 0);
            if (!IsPostBack)
            {
                DataTable dt = new DataTable();
                if (Request["clientid"] == null)
                {
                    //下单过程中初始化身形数据
                    dt = clientbll.getOrderClient(username);
                }
                else
                {
                    //修改客户信息时初始化身形数据
                    dt=clientbll.getClientByID(Request["clientid"].ToString());
                }
                initData(dt);
            }
        }
        protected void initData(DataTable dt)
        { 
            //初始化客户名和身形图片信息
            clientName.Text = dt.Rows[0]["clientName"].ToString();
            idlist.Value = dt.Rows[0]["postureIdList"].ToString();
            this.ClientScript.RegisterStartupScript(this.GetType(),"init","postureInit()",true);
        }
        protected void bindRep(Repeater rep,int parentid)
        {
            string cmdstr = "select * from PosturePicture where parentID="+parentid.ToString();
            SqlHelper.BindRepeater(rep, SqlHelper.SqlConnectionString, CommandType.Text, cmdstr, null);
        }

        protected void repGroup_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Repeater repItem = (Repeater)e.Item.FindControl("repItem");
            int parentid = int.Parse(((HiddenField)e.Item.FindControl("parentID")).Value);
            bindRep(repItem, parentid);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            //保存用户的身形数据
            Client client = new Client();
            client.userName = username;
            client.clientName = clientName.Text.Trim();
            client.postureIdList = idlist.Value;
            clientbll.updatePosture(client);
            if (Request["clientid"] == null)
            {
                Response.Redirect("/Order/OrderItemList.aspx");
            }
            else {
                Response.Redirect("/Manager/ClientManager/Default.aspx");
            }
        }
    }
}